package com.cw.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.cw.auth.application.convert.AuthPermissionDTOConverter;
import com.cw.auth.application.convert.AuthRoleDTOConverter;
import com.cw.auth.application.dto.AuthPermissionDTO;
import com.cw.auth.application.dto.AuthRoleDTO;
import com.cw.auth.common.entity.Result;
import com.cw.auth.domain.entity.AuthPermissionBO;
import com.cw.auth.domain.entity.AuthRoleBO;
import com.cw.auth.domain.service.AuthPermissionDomainService;
import com.cw.auth.domain.service.AuthRoleDomainService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 权限controller
 *
 * @Author:
 * @Date: 2024/4/28 19:06
 */

@RestController
@Slf4j
@RequestMapping("/permission")
public class PermissionController {
    @Resource
    private AuthPermissionDomainService authPermissionDomainService;

    /**
     * @author: 江传文
     * @date: 2024/4/28 20:38
     * <p>
     * 新增权限
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("PermissionController.add.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            // 检查传入的字符串是否为 null、空字符串或者只包含空白字符（空格、制表符、换行符等）
            Preconditions.checkArgument(!StringUtils.isBlank(authPermissionDTO.getName()), "权限名称不能为空");
            Preconditions.checkNotNull(authPermissionDTO.getParentId(), "权限父id不能为空");
            // (DTO -> BO)
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE
                    .convertDtoToBO(authPermissionDTO);

            return Result.ok(authPermissionDomainService.add(authPermissionBO));
        } catch (Exception e) {
            log.error("PermissionController.add.error:{}", e.getMessage(), e);
            return Result.fail("新增权限失败");
        }
    }

    /**
     * @author: 江传文
     * @date: 2024/4/28 20:38
     * <p>
     * 更新权限
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("PermissionController.update.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getId(), "权限id不能为空");
            AuthPermissionBO permissionBO = AuthPermissionDTOConverter.INSTANCE.convertDtoToBO(authPermissionDTO);
            return Result.ok(authPermissionDomainService.update(permissionBO));
        } catch (Exception e) {
            log.error("PermissionController.update.error:{}", e.getMessage(), e);
            return Result.fail("更新权限信息失败");
        }
    }


    /**
     * @author: 江传文
     * @date: 2024/4/28 20:38
     * <p>
     * 删除权限
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("PermissionController.delete.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getId(), "权限id不能为空");
            AuthPermissionBO permissionBO = AuthPermissionDTOConverter.INSTANCE.convertDtoToBO(authPermissionDTO);
            return Result.ok(authPermissionDomainService.delete(permissionBO));
        } catch (Exception e) {
            log.error("PermissionController.delete.error:{}", e.getMessage(), e);
            return Result.fail("删除权限信息失败");
        }
    }

}
